#ifndef JSONRPCSOCKET_H
#define JSONRPCSOCKET_H


#include <QObject>
#include <QMap>
#include <QMutex>

#include "jsonrpcmessage.h"


class QIODevice;
class JsonRpcResponseHandler;
class JsonRpcNotificationHandler;

class JsonRpcSocket : public QObject
{
    Q_OBJECT

public:
    JsonRpcSocket(QIODevice *device, QObject *parent = 0);

public Q_SLOTS:
    void sendMessage(const JsonRpcMessage& message);

    JsonRpcResponseHandler* invokeMethod(const QString& methodName,   const QVariant& arg1 = QVariant(),
                                                            const QVariant& arg2 = QVariant(),
                                                            const QVariant& arg3 = QVariant());

    JsonRpcNotificationHandler *handleNotification(const QString& methodName, QObject *instance, const char *method, bool automaticHandle = true);

Q_SIGNALS:
    void jsonRpcRequestReceived(JsonRpcMessage);
    void jsonRpcNotificationReceived(JsonRpcMessage);
    void jsonRpcResultReceived(JsonRpcMessage);

private Q_SLOTS:
    void processIncomingData();

private:
    QIODevice *p_device;
    QByteArray v_lastObjectPart;
    QMutex v_notificationMutex;

    QMap<int, JsonRpcResponseHandler*> v_currentRequest;
    QMap<QString, JsonRpcNotificationHandler*> v_notificationHandlerMap;
};


#endif // JSONRPCSOCKET_H
